package Operation;

import Book.Book;
import Book.Library;

import java.util.Scanner;

public class DelBook implements IOperation{
    @Override
    public void work(Library library) {
        System.out.println("删除图书");
        if(library.getSize() == 0){
            System.out.println("书架中没书");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删去的书的书字：");
        String name = scanner.nextLine();
        int flg = -1;
        for (int i = 0; i < library.getSize(); i++) {
            if(library.getBook(i).getName().equals(name)){
                flg = i;
                break;
            }
        }
        if(flg == -1){
            System.out.println("不存在这本书");
            return;
        }

        for (int i = flg; i < library.getSize() - 1; i++) {
            Book book = library.getBook(i + 1);
            library.setBook(i,book);
        }
        System.out.println("删除成功");
        library.setSize(library.getSize() - 1);
        ShowBook showBook = new ShowBook();
        showBook.work(library);
    }
}
